function h5camera() {
    var timer;
    var sourceBuffer;
    var buffer = [];
    var mediaSource;
    var video;
    var wsSocket;
    var chn;
    var path;
    var self = this;
    this.attach = function (id, path, chn) {
        if (self.wsSocket != null) {
            self.wsSocket.close();
        }
        self.mediaSource = new window.MediaSource();
        self.video = document.getElementById(id);
        self.chn = chn;
        self.path = path;
        self.video.src = window.URL.createObjectURL(self.mediaSource);
        self.mediaSource.addEventListener('sourceopen', function () {
            if (self.mediaSource.activeSourceBuffers.length == 0) {
                var strCodec = 'video/mp4; codecs="avc1.640029"';
                self.sourceBuffer = self.mediaSource.addSourceBuffer(strCodec);
                self.mediaSource.duration = Infinity;
                self.sourceBuffer.addEventListener('updateend', self.readFromBuffer);

                if (self.wsSocket != null)
                    self.wsSocket.close();
                self.wsSocket = self.getWebSocketClient(self.path);
                self.wsSocket.onopen = self.onWebSocketOpen;
                self.wsSocket.onmessage = self.onWebSocketData;
            }
        });
    };
    this.isSupport = function () {
        window.MediaSource = window.MediaSource || window.WebKitMediaSource;
        if (!window.MediaSource) {
            console.log('MediaSource API is not available');
            return false;
        }
        return true;
    }
    this.readFromBuffer = function () {
        if (buffer.length === 0 || !self.sourceBuffer || self.sourceBuffer.updating) {
            return;
        }
        try {
            var data = buffer.shift();
            self.sourceBuffer.appendBuffer(data);
        } catch (e) {
            console.log(e);
        }
    };
    this.getWebSocketClient = function(path) {
        var socket;
        try {
            if (window.location.protocol == "http:") {
                socket = new WebSocket('ws://' + window.location.host + path);
            }
            if (window.location.protocol == "https:") {
                console.log(window.location.host);
                socket = new WebSocket('wss://' + window.location.host + path);
            }
        } catch (e) {
            alert(e);
            return;
        }
        return socket;
    }
    this.onWebSocketOpen = function(evt) {
        self.wsSocket.send("{'c':'open','ch':'"+self.chn+"','a':'true'}");
    }
    this.onWebSocketData = function (msg) {
        if ((typeof msg.data === 'string') && msg.data.constructor == String) {
            console.log(msg.data);
            return;
        }
        var blob = msg.data;

        var fileReader = new FileReader();
        fileReader.onload = function() {
            buffer.push(this.result);
            self.readFromBuffer();
        };

        fileReader.readAsArrayBuffer(blob);
    };
}